<?php
/**
 * 字段服务层
 * @author maniac
 */
namespace app\common\service;

class Fields extends Base
{
    // 查询字段
    public static $field = 'id,f,fname,fform,pattern,list_order';
    
    /**
     * 获取排序条件
     * @param  string $order 排序条件
     * @return string
     */
    protected static function getOrder($order)
    {
        return $order ?: 'list_order ASC, id ASC';
    }
    
    /**
     * 格式化字段值
     * @param  array $data   请求的数据
     * @param  array $fields 字段信息
     * @return array
     */
    public static function formatData($data, $fieldss)
    {
        foreach ($fieldss as $field) {
            switch ($field['fform']) {
                case 'timestamp(Y-m-d)': case 'timestamp(Y-m-d H:i:s)':
                    $data[$field['f']] = strtotime($data[$field['f']]);
                    break;
                case 'checkbox':
                    $data[$field['f']] = isset($data[$field['f']]) ? implode(',', $data[$field['f']]) : '';
                    break;
                case 'image':
                    $data[$field['f']] = (isset($data[$field['f']]) && $data[$field['f']]) ? $data[$field['f']] : '';
                    break;
                case 'file':
                    $arr = [];
                    if (isset($data[$field['f']]['name'])) {
                        foreach ($data[$field['f']]['name'] as $key => $val) {
                            $arr[$key] = [
                                'value' => $data[$field['f']]['value'][$key],
                                'name'  => $val,
                            ];
                        }
                        $data[$field['f']] = serialize($arr);
                    } else {
                        $data[$field['f']] = '';
                    }
                    break;
                case 'photos':
                    $arr = [];
                    if (isset($data[$field['f']]) && !empty($data[$field['f']])) {
                        foreach ($data[$field['f']] as $val) {
                            $arr[] = $val;
                        }
                        $data[$field['f']] = serialize($arr);
                    } else {
                        $data[$field['f']] = '';
                    }
                    break;
                case 'photos_with_txt':
                    $arr = [];
                    if (isset($data[$field['f']]) && !empty($data[$field['f']])) {
                        foreach ($data[$field['f']]['desc'] as $key => $val) {
                            $arr[$key] = [
                                'value' => $data[$field['f']]['value'][$key],
                                'desc'  => $val,
                            ];
                        }
                        $data[$field['f']] = serialize($arr);
                    } else {
                        $data[$field['f']] = '';
                    }
                    break; 
                case 'editor':
                    $data[$field['f']] = addslashes($data[$field['f']]);
            }
        }
        return $data;
    }
}
